import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import java.awt.Font;
/*You can buy or not buy, none auctioning !*/

public class PropertyUI extends JPanel {
	private BoardUi board;
	private JButton btnBuy, btnNotBuy, btnPay;
	private JLabel label;
	private Player p;
	private Block[] bl;
	private Property property;
	private Property[] propArray;
	private int currentRent;
	private boolean ownsAll;
	
	public PropertyUI(Player currentPlayer, Property prop, Block[] blk, BoardUi board_) {
		this.p = currentPlayer;
		this.property = prop;
		this.bl=blk;
		this.propArray = new Property[22];
		this.board=board_;
		
		this.setLayout(null);
		
		label = new JLabel("");
		label.setIcon(new ImageIcon(getClass().getResource(this.property.getPropertyDeed())));
		label.setBounds(0, 0, 199, 200);
		label.setVisible(true);
		this.add(label);
		
		btnBuy = new JButton("Bilin");
		btnBuy.setFont(new Font("Tahoma", Font.BOLD, 11));
		btnBuy.setBounds(199, 68, 100, 23);
		btnBuy.setEnabled(false);
		btnBuy.setVisible(false);
		btnBuy.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				buy();
			}
		});
		this.add(btnBuy);
		
		btnPay = new JButton("Renta");
		btnPay.setFont(new Font("Tahoma", Font.BOLD, 11));
		btnPay.setBounds(199, 103, 100, 23);
		btnPay.setEnabled(false);
		btnPay.setVisible(false);
		btnPay.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				pay();
			}
		});
		this.add(btnPay);
		
		btnNotBuy = new JButton("Return");
		btnNotBuy.setFont(new Font("Tahoma", Font.BOLD, 11));
		btnNotBuy.setBounds(199, 137, 100, 23);
		btnNotBuy.setEnabled(false);
		btnNotBuy.setVisible(false);
		btnNotBuy.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				returnToGame();
			}
		});
		this.add(btnNotBuy);
		
		this.checkOwner();
		this.fillPropertyArray();
		this.getOwnedPropertyGroup();
		this.getCurrentRent();
	}
	
	public void buy() {
		if(board.getCurrentPlayer().getMoney()>= property.getPrice()){
		property.setOwner(p);
		p.subtractMoney(property.getPrice());
		btnBuy.setEnabled(false); //you can only buy it one time
		btnBuy.setVisible(false);
		board.setCurrentPlayer();
		board.getPlayerPanelUI().updatePlayerMoney();
		board.getPlayerPanelUI().updateButtons(this.p.getPlayerCount(), this.p.getCurrentPosition());
		}else{
			JOptionPane.showMessageDialog(null,"You don't have enough Money to buy!");
		}
	}
	
	public void returnToGame(){
		setVisible(false);
		board.getDiceUI().rolledDoubles(); //better people can't pass turn just cuz they rolled Doubles
	//	board.getGameButtonUI().enableEndTurnBtn();

	}
	
	public void pay(){
		if(board.getCurrentPlayer().getMoney()>= currentRent){
		p.subtractMoney(currentRent);
		this.property.getOwner().addMoney(currentRent);
		btnPay.setEnabled(false);	//you can pay one time per turn
		btnPay.setVisible(false);
		btnNotBuy.setEnabled(true);
		btnNotBuy.setVisible(true);
		board.setCurrentPlayer();
		board.getPlayerPanelUI().updatePlayerMoney();
		}else{
			BankruptcyChecker bc = new BankruptcyChecker(p, bl);
			int worth=bc.checkWorth();
			
			if(worth>=currentRent){
				JOptionPane.showMessageDialog(null,"You don't have enough Money to pay rent, just mortgage your properties");
			}else{
				bc.transferAssetsPlayer();
				board.getCurrentPlayer().setActive(false);
				JOptionPane.showMessageDialog(null,"You don't have enough Money to pay rent, Out ka na!");
				btnNotBuy.setVisible(true);
				
			}
			
		}
	}
	
	public void checkOwner(){
		if(this.property.getOwner()==null){
			btnBuy.setEnabled(true);
			btnBuy.setVisible(true);
			btnNotBuy.setEnabled(true);
			btnNotBuy.setVisible(true);
		}else if(this.property.getOwner()!=this.p){
			btnPay.setEnabled(true);
			btnPay.setVisible(true);
		}else if(this.property.getOwner()==this.p){
			btnNotBuy.setEnabled(true);
			btnNotBuy.setVisible(true);
		}
	}
	
	public void fillPropertyArray(){
		int propCounter = 0;
		for(int i=0;i<this.bl.length;i++){
			if(this.bl[i] instanceof Property){
				this.propArray[propCounter]=(Property)bl[i];
				propCounter++;
			}
		}
	}
	
	public void getOwnedPropertyGroup(){
		int ownedCounter = 0;
		
		for(int i=0; i<this.propArray.length; i++){
			if(this.propArray[i].getPropertyGroup()==this.property.getPropertyGroup()){//match fist
				if(this.propArray[i].getOwner()==this.property.getOwner() && this.property.getOwner()!=null){//match second
					ownedCounter++;
				}
			}
		}
		if(this.property.getNumberOfPropertiesInGroup()==ownedCounter){//might need to subtract 1 from number of properties in group
			this.ownsAll=true;
		}else{
			this.ownsAll=false;
		}
	}
	
	public void getCurrentRent(){
		if(this.property.getNumHouse()==5){
			this.currentRent=this.property.getRent5();
		}else if(this.property.getNumHouse()==4){
			this.currentRent=this.property.getRent4();
		}else if(this.property.getNumHouse()==3){
			this.currentRent=this.property.getRent3();
		}else if(this.property.getNumHouse()==2){
			this.currentRent=this.property.getRent2();
		}else if(this.property.getNumHouse()==1){
			this.currentRent=this.property.getRent1();
		}else if(this.property.getNumHouse()==0){
			if(ownsAll==true)
				this.currentRent=this.property.getRent0()*2;
			else
				this.currentRent=this.property.getRent0();	
		}	
	}

}
